package com.cn.lyl.service.impl;

import com.cn.lyl.dao.LawyerDao;
import com.cn.lyl.dao.LawyerRoleDao;
import com.cn.lyl.dao.LawyerRoleDependenceDao;
import com.cn.lyl.dao.impl.LawyerDaoImpl;
import com.cn.lyl.dao.impl.LawyerRoleDaoImpl;
import com.cn.lyl.dao.impl.LawyerRoleDependenceDaoImpl;
import com.cn.lyl.model.LawyerModel;
import com.cn.lyl.model.LawyerRoleDependenceModel;
import com.cn.lyl.model.LawyerRoleModel;
import com.cn.lyl.model.UserModel;
import com.cn.lyl.service.LawyerService;

import java.util.List;

public class LawyerServiceImpl implements LawyerService {

    @Override
    public List<LawyerModel> findAllLawyer(LawyerModel lawyerModel) {
        LawyerDao lawyerDao = new LawyerDaoImpl();

        return lawyerDao.findAll(lawyerModel);
    }

    @Override
    public int getCount(LawyerModel lawyerModel) {
        LawyerDao lawyerDao = new LawyerDaoImpl();
        return lawyerDao.countFindAll(lawyerModel);
    }

    @Override
    public String addLawyer(LawyerModel lawyerModel) {
        LawyerDao lawyerDao = new LawyerDaoImpl();
        LawyerRoleDao lawyerRoleDao = new LawyerRoleDaoImpl();
        LawyerRoleDependenceDao lawyerRoleDependenceDao = new LawyerRoleDependenceDaoImpl();
        LawyerRoleModel role = new LawyerRoleModel();
        if(lawyerModel.getDependenceObj() ==null){
            return lawyerDao.add(lawyerModel)>0?"success":"error";
        }
        int insert = lawyerDao.add(lawyerModel);
        role.setRole((String) lawyerModel.getDependenceObj());
        LawyerRoleModel byName = lawyerRoleDao.findByName(role);
        LawyerModel newModel = lawyerDao.findNewest();
        LawyerRoleDependenceModel model = new LawyerRoleDependenceModel();
        model.setLawyerId(newModel.getId());
        model.setRoeId(byName.getId());
        return (lawyerRoleDependenceDao.add(model)>0&&insert>0)?"success":"error";


    }

    @Override
    public String deleteLawyer(LawyerModel lawyerModel) {
        LawyerDao lawyerDao = new LawyerDaoImpl();
        LawyerRoleDependenceDao lawyerRoleDependenceDao = new LawyerRoleDependenceDaoImpl();
        LawyerRoleDependenceModel lawyerRoleDependenceModel=new LawyerRoleDependenceModel();
        lawyerRoleDependenceModel.setLawyerId(lawyerModel.getId());
        int deleteTemp = lawyerRoleDependenceDao.delete(lawyerRoleDependenceModel);
        return lawyerDao.delete(lawyerModel)>0?"success":"error";
    }

    @Override
    public LawyerModel findInfo(int id) {
        LawyerDao lawyerDao = new LawyerDaoImpl();
        return lawyerDao.findById(id);
    }

    @Override
    public String changeInfo(LawyerModel lawyerModel) {
        LawyerDao lawyerDao = new LawyerDaoImpl();
        return lawyerDao.update(lawyerModel)>0?"success":"error";
    }

    @Override
    public String changeHomeState(LawyerModel lawyerModel) {
        LawyerDao lawyerDao = new LawyerDaoImpl();
        return lawyerDao.updateIsHome(lawyerModel)>0?"success":"error";
    }


}
